package com.example.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhxy.model.LoginInfo;
import com.example.zhxy.model.Teacher;
import com.example.zhxy.service.TeacherService;
import com.example.zhxy.mapper.TeacherMapper;
import com.example.zhxy.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
* @author fzykd
* @description 针对表【tb_teacher】的数据库操作Service实现
* @createDate 2023-03-13 21:26:35
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService{

    @Override
    public Teacher login(LoginInfo loginInfo) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", loginInfo.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginInfo.getPassword()));
        return baseMapper.selectOne(queryWrapper);

    }


    @Override
    public Teacher getTeacherById(Long userId) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        return baseMapper.selectOne(queryWrapper);
    }


    //教师分页查询
    @Override
    public IPage<Teacher> getTeacherPage(Page<Teacher> page, String name, String clazzName) {
        //条件构造器
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(clazzName),Teacher::getClazzName,clazzName);
        queryWrapper.like(!StringUtils.isEmpty(name),Teacher::getName,name);

        //ASC升序 DESC降序
        queryWrapper.orderByAsc(Teacher::getId);

        return baseMapper.selectPage(page,queryWrapper);
    }
}




